package com.pucheng.bracelet.viewmodel.steps

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.clj.fastble.exception.BleException
import com.pucheng.bracelet.app.ble.BleCmd
import com.pucheng.bracelet.app.ble.BleWriteAndNotifyCallback
import com.pucheng.bracelet.app.ble.ClientManager
import com.pucheng.bracelet.app.database.repository.DBRepository
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.data.bean.StepsBean
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import com.pucheng.bracelet.app.util.DatetimeUtil
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.launch
import me.hgj.jetpackmvvm.ext.util.loge
import me.hgj.jetpackmvvm.state.ResultState
import me.hgj.jetpackmvvm.state.paresException
import me.hgj.jetpackmvvm.state.paresResult

class StepsViewModel : BaseViewModel() {


    private val repository: DBRepository = DBRepository()
    var queryDate = MutableLiveData<String>()

    var stepsData = MutableLiveData<StepsBean>()
    var stepsTo7Day: LiveData<List<StepsBean>> = repository.allDataTo7DayToStep.asLiveData()
    var allDataToStep: LiveData<List<StepsBean>> = repository.allDataToStep.asLiveData()
    val weekStepsData = MutableLiveData<ArrayList<StepsBean>>()
    private val weekSteps: ArrayList<StepsBean> = arrayListOf()
    private val monthsSteps: ArrayList<StepsBean> = arrayListOf()
    val stepsDuration = MutableLiveData<Int>()

    var stepsTager = MutableLiveData<Int>().apply {
        value = CacheUtil.getStepsFlag().toInt()
    }

    fun queryDateToDate(){

        launch({
            repository.getStepsInfoToDate2(queryDate.value!!)
        },{
            if (it != null) {
                stepsData.value = it
            }else{
                stepsData.value = StepsBean()
            }
        },{})
    }

    fun getSteps() {
        ClientManager.getInstance()
            .sendControlCmd(BleCmd.GET_HIS_STEPS_INFO, object : BleWriteAndNotifyCallback() {
                override fun onCharacteristicChanged(data: ByteArray?) {

                }

                override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
                }

                override fun onWriteFailure(exception: BleException?) {
                }
            })
    }

    fun getWeekDate() {
        weekSteps.clear()
        val index: Int = DatetimeUtil.getWeekIndexToDay() - 1
        LogUtils.e("weekIndex:$index")
        var duration = 0
        if (index == 0 || !stepsTo7Day.value.isNullOrEmpty()) {
            for (i in 0 until index) {
                val date = DatetimeUtil.getOldDate(i - index)
                val stepsIndex = stepsTo7Day.value!!.indexOfFirst { date == it.date }
                //            val stepsData = stepsTo7Day.value!![stepsIndex]
                if (stepsIndex != -1) {
                    duration += stepsTo7Day.value!![stepsIndex].time.toInt()
                    weekSteps.add(stepsTo7Day.value!![stepsIndex])
                } else {
                    weekSteps.add(StepsBean(date))
                }
            }
        }
        if (stepsData.value != null) {
            weekSteps.add(stepsData.value!!)
            duration += stepsData.value!!.time.toInt()
        }else{
            weekSteps.add(StepsBean())
        }
        stepsDuration.value = duration
        for (i in 1..(6 - index)) {
            weekSteps.add(StepsBean(DatetimeUtil.getOldDate(DatetimeUtil.formatDate(queryDate.value!!,DatetimeUtil.DATE_PATTERN), i)))
        }
        weekStepsData.value = weekSteps
    }
}